package com.neu.ui;

import javax.servlet.http.HttpSession;

import com.neu.po.User;
import com.neu.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;


@Controller
public class LoginController {
	@Autowired
	private LoginService service;
	@GetMapping("/login")
	public String login(){
		System.out.println("Enter Login");
		return "login";
	}
	@PostMapping("/login")
	public String login(String name,String pwd,HttpSession session,Model model){
		User user=service.login(name,pwd);
		if(user !=null){
			session.setAttribute("user",user);
			return "redirect:/index";
		}
		model.addAttribute("err","用户名或密码不符");
		return "login";
	}
	@GetMapping("/register")
	public String add(){
		return "register";
	}
	@PostMapping("/register")
	public String add(String name,String loginName,String loginPwd,float balance,String phone){
		service.add(name,loginName, loginPwd,balance,phone);
		return "redirect:/login";
	}
}
